package com.gpf.dao.impl;

import com.gpf.dao.CardDao;
import com.gpf.entity.Book;
import com.gpf.entity.Card;
import com.gpf.entity.User;
import com.gpf.utils.BaseDao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class CardDaoImpl implements CardDao {

    /**
     * 通过用户Id获取购物车信息
     * @param id
     * @return
     */
    @Override
    public List<Card> getCard(Integer id) {
        Connection con = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            // 1.获取数据库连接
            con = BaseDao.getConnection();
            // 2.写sql
            String sql = "select * from card where userid = ?";
            // 3.预编译
            ps = con.prepareStatement(sql);
            // 4.设置值
            ps.setObject(1,id);
            rs = ps.executeQuery();
            List<Card> list = new ArrayList<Card>();
            while(rs.next()){
                Card card = new Card();
                // 从数据库中获取值到实体类的setter方法中
                card.setId(rs.getInt("id"));
                card.setUserid(rs.getInt("userid"));
                card.setUsername(rs.getString("username"));
                card.setBookid(rs.getInt("bookid"));
                card.setBooknum(rs.getInt("booknum"));

                // 测试日志
                System.out.print("查看用户书籍-->");
                System.out.println(card);
                // 将对象保存到list中
                list.add(card);
            }
            return list;
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            // 关闭资源，避免出现异常
            BaseDao.close(con,ps,rs);
        }
        return null;
    }

    /**
     * 添加购物信息
     * @param user
     * @param book
     * @param booknum
     * @return
     */
    @Override
    public boolean addCard(User user, Book book, Integer booknum) {
        boolean flag = false;
        // 判断之前购物车里面是否有图书,获取图书数量
        CardDao dao = new CardDaoImpl();
        Integer bookNumBefore = dao.getBookNum(book);

        // 判断图书数量为0，则添加图书信息
        if(bookNumBefore == 0){
            String bookName = book.getBookname();
            System.out.print("本次添加图书: "+bookName+" 之前图书的数量：");
            System.out.println(bookNumBefore);
            String sql = "insert into card values (0,?,?,?,?)";
            List<Object> list = new ArrayList<Object>();
            list.add(user.getId());
            list.add(user.getName());
            list.add(book.getBookid());
            list.add(booknum);
            flag = BaseDao.addUpdateDelete(sql,list.toArray());
        }else{ // 更新图书数量信息
            String bookName = book.getBookname();
            System.out.print("本次更新图书: "+bookName+" 之前图书的数量：");
            System.out.println(booknum);
            String sql = "update card set booknum = ? where bookid = ?";
            List<Object> list = new ArrayList<Object>();
            list.add(booknum);
            list.add(book.getBookid());
            flag = BaseDao.addUpdateDelete(sql,list.toArray());
        }

        // 返执行结果
        if(flag){
            return true;
        }else {
            return false;
        }
    }

    /**
     * 根据Book获取booknum
     * @param book
     * @return
     */
    @Override
    public Integer getBookNum(Book book) {
        Integer booknum = 0;
        Integer bookid = book.getBookid();
        Connection con = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            // 1.获取数据库连接
            con = BaseDao.getConnection();
            // 2.写sql
            String sql = "select booknum from card where bookid = ?";
            // 3.预编译
            ps = con.prepareStatement(sql);
            // 4.设置值
            ps.setObject(1,bookid);
            rs = ps.executeQuery();
            List<Card> list = new ArrayList<Card>();
            if(rs.next()){
                booknum = rs.getInt(1);
            }
            return booknum;
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            // 关闭资源，避免出现异常
            BaseDao.close(con,ps,rs);
        }
        return null;
    }

}

